package com.zsc.wxapp.mapper;

import com.zsc.wxapp.entity.IPriceTag;
import com.zsc.wxapp.entity.PriceTag;
import com.zsc.wxapp.entity.PriceTagSQLResult;
import com.zsc.wxapp.entity.dto.PriceTagDTO;
import com.zsc.wxapp.entity.external.PriceNameData;
import com.zsc.wxapp.entity.external.ProductDetails;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
@Repository
@Mapper
public interface PriceTagMapper {
    int deleteByPrimaryKey(String pturl_uuid);

    int insert(IPriceTag record);

    int insertSelective(IPriceTag record);

    IPriceTag selectByPrimaryKey(String pturl_uuid);

    int updateByPrimaryKeySelective(IPriceTag record);

    int updateByPrimaryKey(IPriceTag record);

    List<IPriceTag> findAll();

    @Select("SELECT product_uuid FROM cigarette_info WHERE brand = #{productName}")
    String findProductUuidByProductName(String productName);

    @Select("SELECT recognized_price FROM Price_Tag WHERE product_name = #{productName}")
    Double findUserRetailPriceByProductName(String productName);

    @Select("SELECT product_upload_date, actual_retail_price " +
            "FROM cigarette_info " +
            "WHERE product_upload_date >= CURDATE() - INTERVAL 90 DAY")
    List<PriceNameData> getPriceData(String productUuid);

    @Select("SELECT direct_retail_price, whole_sale_price, is_province, product_type_code, " +
            "price_type_code_text,  brand_uuid, tar_qty, " +
            "nicotine_qty, spec FROM cigarette_info WHERE product_uuid = #{productUuid}")
    List<ProductDetails> getProductDetails(String productUuid);

    @Insert({
            "<script>",
            "INSERT INTO price_tag (pt_uuid, cust_uuid, pic_urlid, pt_type, product_uuid, pt_create_date, ",
            "recognized_price, product_name, direct_retail_price, cigarette_total_info_uuid, cust_total_info_uuid) VALUES ",
            "<foreach collection='priceTags' item='priceTag' separator=','>",
            "(#{priceTag.pt_uuid}, #{priceTag.cust_uuid}, #{priceTag.pic_urlid}, #{priceTag.pt_type}, ",
            "#{priceTag.product_uuid}, #{priceTag.pt_create_date}, #{priceTag.recognized_price}, ",
            "#{priceTag.product_name}, #{priceTag.direct_retail_price}, #{priceTag.cigarette_total_info_uuid}, ",
            "#{priceTag.cust_total_info_uuid})",
            "</foreach>",
            "</script>"
    })
    void insertPriceTags(List<IPriceTag> priceTags);

    /**
     * 插入一条价签数据
     * @param priceTag
     */
    void insertPriceTag(PriceTag priceTag);

    /**
     * 获取用户上次识别价签的时间
     * @param userId
     * @return
     */
    PriceTagSQLResult getDate(Integer userId);

    /**
     * 根据用户id获取用户识别价签数据
     * @param userId
     * @return
     */
    List<PriceTag> getAllByUserId(Long userId);

    /**
     * 根据用户id和识别时间获取用户识别价签数据
     * @param priceTagDTO
     * @return
     */
    PriceTag getPriceTag(PriceTagDTO priceTagDTO);

    /**
     * 根据用户id获取最近一次上传价签数据
     * @param userId
     * @return
     */
    List<PriceTag> getPriceTagByUserId(Long userId);

    /**
     * 根据价签id修改价签数据
     * @param priceTagDTO
     */
    void updatePriceTagById(PriceTagDTO priceTagDTO);

    /**
     * 获取到用户最新的价签数据
     * @param userId
     * @return
     */
    List<PriceTag> getNewPriceTagsByUserId(Long userId);

    /**
     * 根据商品名称搜索价签
     * @param name 卷烟名称
     * @return List
     */
    List<PriceTag> searchPriceTagByName(Long userId, String name);

    /**
     * 根据价签id删除价签数据
     * @param id
     */
    @Delete("delete from price_tag where price_tag_id = #{id}")
    void deleteById(Long id);

    /**
     * 更加价签id获取价签数据
     * @param id
     * @return
     */
    @Select("select * from price_tag where price_tag_id = #{id}")
    PriceTag getPriceTagById(Long id);
}